package com.situ.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.CusDevPlan;
import com.situ.crm.entity.Product;
import com.situ.crm.mapper.CusDevPlanMapper;
import com.situ.crm.service.ICusDevPlanService;

@Service
public class CusDevPlanServiceImpl implements ICusDevPlanService {
	@Autowired
	private CusDevPlanMapper cusDevPlanMapper;
	
	@Override
	public List<CusDevPlan> select(Integer id) {
		return cusDevPlanMapper.selectById(id);
	}

	//分页
	@Override
	public ServerResponse pageList(Integer limit, Integer page, Integer id) {
		PageHelper.startPage(page,limit);
		List<Product> list = cusDevPlanMapper.pageList(id);
		
		PageInfo pageInfo = new PageInfo<>(list);
		Integer count = (int)pageInfo.getTotal();
		return ServerResponse.createSuccess("查找成功", count, list);
	}

	//添加功能
	@Override
	public ServerResponse insert(CusDevPlan cusDevPlan) {
		try {
			Integer count = cusDevPlanMapper.insert(cusDevPlan);
			if (count == 1) {
				return ServerResponse.createSuccess("添加成功");
			} else {
				return ServerResponse.createError("添加失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("添加失败");
		}
	}

	//删除功能
	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			Integer count = cusDevPlanMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除 失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//批量删除
	@Override
	public ServerResponse deleteAll(String ids) {
		String[] idArrays = ids.split(",");
		try {
			Integer count = cusDevPlanMapper.deleteAll(idArrays);
			if (count >= 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//修改前查询
	@Override
	public CusDevPlan selectById(Integer id) {
		return cusDevPlanMapper.selectByPrimaryKey(id);
	}

	@Override
	public ServerResponse updata(CusDevPlan cusDevPlan) {
		try {
			Integer count = cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan);
			if (count == 1) {
				return ServerResponse.createSuccess("修改成功");
			} else {
				return ServerResponse.createError("修改失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("修改失败");
		}
	}

}
